import { useFilePidStore } from '@/stores/modules/filePid'
import { useDataStore } from '@/stores/modules/tableData'
import instance from '@/util/api'

//返回上一级
export const gotoUpFoloder = async (filePid) => {
  await instance.get(`/file/gotoUpFolder/${filePid}`).then((res) => {
    console.log(res)
    useDataStore().setData(res.data)
    localStorage.setItem('filePid', res.data[0].filePid)
    useFilePidStore().setFilePid(res.data[0].filePid)
  })
}

//创建新文件夹
export const newFolderService = async (userId, filePid, fileName) => {
  const res = await instance.post('/file/newFoloder', {
    userId,
    filePid,
    fileName
  })
  // enterFolder(localStorage.getItem('filePid')).then((res) => {
  //   console.log(res)
  //   useDataStore().setData(res.data)
  // })
  return res.data
}
// 根据文件类型查询
export const selectFileType = async (userId, fileCategory) => {
  const res = await instance.get('/file/FileCategory', {
    params: { userId, fileCategory }
  })
  return res
}
//搜索文件
export const selectFile = async (userId, fileName) => {
  const res = await instance.get('/file/searchFile', {
    params: { userId, fileName }
  })
  console.log(res)
  return res
}
//进入文件夹
export const enterFolder = async (filePid) => {
  const res = await instance.get(`/file/loadAllFolder/${filePid}`)
  console.log(res)
  return res
}

// ----------------------
//单文件分享
export const singleShareService = async (data) => {
  const res = await instance.post('/share', data)
  return res.data
}
//单文件下载
export const singledownloadService = async (id) => {
  const res = await instance.post('/download', id)
  return res.data
}
//单文件删除
export const singleDeleteService = async (id) => {
  const res = await instance.post('/delete', id)
  return res.data
}
//文件重命名
export const renameService = async (id, name) => {
  const res = await instance.post('/rename', { id, name })
  return res.data
}
//单文件移动
export const singleMoveService = async (id, path) => {
  const res = await instance.post('/move', { id, path })
  return res.data
}

// 文件上传
export const uploadService = async (file) => {
  const res = await instance.post('/upload', file)
  return res.data
}

//文件批量下载
export const batchDownloadService = async (data) => {
  const res = await instance.post('/batchdownload', data)
  return res.data
}
//文件批量删除
export const batchDeleteService = async (id) => {
  const res = await instance.post('/batchdelete', id)
  return res.data
}
//文件批量移动
export const batchMoveService = async (data, path) => {
  const res = await instance.post('/batchmove', { data, path })
  return res.data
}
// import axios from 'axios'

// const Authorization = 'Bearer ' + localStorage.getItem('Authorization')
// const commonService = axios.create({
//   baseURL: 'http://47.113.219.77:10010',
//   timeout: 6000,
//   headers: {
//     'Content-Type': 'application/json;charset=utf-8',
//     Authorization: Authorization
//   }
// })

// // // // 请求拦截
// // commonService.interceptors.request.use((config) => {
// //     config.headers = config.headers || {}
// //     if (localStorage.getItem('Authorization')) {
// //         config.headers.Authorization = localStorage.getItem('Authorization') || ""
// //     }
// //     return config
// // })

// // // //响应拦截
// // commonService.interceptors.response.use((res) => {
// //     const code = res.data.code
// //     // console.log(res);
// //     if (code != 200) {
// //         return Promise.reject(res.data.code)     //失败提示
// //     }
// //     return res
// // }, (err) => {
// //     console.log(err)
// // })

// export default commonService

//会员支付

export const memberPayService = async (levelId, userId) => {
  return await instance.get('/api/buy', { params: { levelId, userId } })
}

//会员订单
export const memberOrderService = async (levelId) => {
  return await instance.get('/api/order', { params: { levelId } })
}
